﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YRevitJig
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class JigCurves : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			ViewAssist.ViewValidating(activeUIDocument.Document, activeUIDocument.ActiveView);
			XYZ xyz = null;
			List<Curve> list = new List<Curve>();
			try
			{
                IList<Reference> list2 = activeUIDocument.Selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1);
				foreach (Reference reference in list2)
				{
					DetailCurve detailCurve = activeUIDocument.Document.GetElement(reference) as DetailCurve;
					bool flag = detailCurve != null;
					if (flag)
					{
						list.Add(detailCurve.GeometryCurve);
					}
				}
				xyz = activeUIDocument.Selection.PickPoint("请选择基点<退出>:");
			}
			catch
			{
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			XCurvesJig xcurvesJig = new XCurvesJig(activeUIDocument, false);
			XYZ xyz2 = xcurvesJig.DragCurves(list, xyz, "请确定插入点：");
			bool flag2 = xyz2 == null;
			Result result;
			if (flag2)
			{
				xcurvesJig.Dispose();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			else
			{
				Transform transform = Transform.CreateTranslation(xyz2 - xyz);
				Transaction transaction = new Transaction(activeUIDocument.Document);
				transaction.Start("CreateCurve");
				foreach (Curve curve in list)
				{
					Curve curve2 = curve.CreateTransformed(transform);
					activeUIDocument.Document.Create.NewDetailCurve(activeUIDocument.ActiveView, curve2);
				}
				transaction.Commit();
				xcurvesJig.Dispose();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}
	}
}
